//
//  PaymentMethedVC.m
//  WalletAPP
//
//  Created by 罗弟祥 on 2019/10/9.
//  Copyright © 2019 罗弟祥. All rights reserved.
//

#import "PaymentMethedVC.h"
#import "PaymentMethedCell.h"
#import "NaviView.h"
#import "AddPaymentMethedView.h"
#import "AddAlipayVC.h"
#import "AddBankTransferVC.h"

#import "PaymentMethodModel.h"
@interface PaymentMethedVC ()<UITableViewDataSource, UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, retain) NaviView *naviView;

@property (nonatomic, retain) AddPaymentMethedView *addpayView;

@property (nonatomic, retain) RefreshVM *vm;


@end

@implementation PaymentMethedVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    weakself
    [self.view addSubview:self.naviView];
    self.naviView.frame = CGRectMake(0, 0, screenW, StatusBarHeight + NavBarHeight);
    self.naviView.titStr = @"Payment Method";
    [self.naviView.rightBtn setImage:[UIImage imageNamed:@"icon_paytype_add"] forState:UIControlStateNormal];
    [self.naviView.rightBtn addTarget:self action:@selector(rightBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    self.naviView.rightBtn.hidden = NO;
    [self.naviView setBackBlock:^{
        [ws.navigationController popViewControllerAnimated:YES];
    }];
    
    
    
    [self configViewModel];
    
}

-(void)configViewModel{
    
    __weak __typeof(&*self) ws = self;
    self.vm = [RefreshVM new];
    self.vm.urlStr = @"/api/payments";
    self.vm.params = [self configParam];
    self.vm.dataClass = [PaymentMethodModel class];
    self.vm.dataArray = [NSMutableArray new];
    [self.vm getRequestDataNormal];
    [self.vm setRequestResultBlock:^(id obj) {
        [ws.vm.dataArray addObjectsFromArray:obj];
        [ws.tableView reloadData];
        
    }];
}
-(NSDictionary *)configParam{
    return @{};
}



-(void)rightBtnClick:(UIButton *)sender{
    [self setPaymentViewBlock];
}
-(NaviView *)naviView{
    if (!_naviView) {
        _naviView = [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([NaviView class]) owner:self options:nil].firstObject;
    }
    return _naviView;
}




- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.vm.dataArray.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return UITableViewAutomaticDimension;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.1;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return nil;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return nil;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    PaymentMethedCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (!cell) {
        cell = [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([PaymentMethedCell class]) owner:self options:nil].firstObject;
    }
    
    [cell loadData:self.vm.dataArray[indexPath.row] indexPath:indexPath];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    
}


-(void)setPaymentViewBlock{
    weakself
    [self.addpayView setCellBlock:^(NSInteger index) {
        [UIView animateWithDuration:0.5 animations:^{
            ws.addpayView.frame = CGRectMake(0, screenH, screenW, screenH);
            [ws.view layoutIfNeeded];
            [ws.view setNeedsLayout];
        }];
    }];
    
    [self.view addSubview:self.addpayView];
    self.addpayView.frame = CGRectMake(0, screenH, screenW, screenH);
    
    [UIView animateWithDuration:0.6 animations:^{
        self.addpayView.frame = CGRectMake(0, NavBarHeight, screenW, screenH);
        [self.view layoutIfNeeded];
        [self.view setNeedsLayout];
    } completion:^(BOOL finished) {
        //        [self shakeAnimationForView:self.hView];
        self.addpayView.frame = CGRectMake(0, 0, screenW, screenH);
        [UIView animateWithDuration:0.3 animations:^{
            [self.view layoutIfNeeded];
            [self.view setNeedsLayout];
        }];
    }];
    
    
    
   
    
}





-(AddPaymentMethedView *)addpayView{
    if (!_addpayView) {
        weakself
        _addpayView = [[NSBundle mainBundle] loadNibNamed:@"AddPaymentMethedView" owner:self options:nil].firstObject;
        [_addpayView setCellItemBlock:^(NSIndexPath * _Nonnull indexpath) {
            if (indexpath.row == 0) {
                AddBankTransferVC *vc = [AddBankTransferVC new];
                [vc setAddSuccessBlcok:^{
                    [ws.vm.dataArray removeAllObjects];
                    [ws.vm getRequestDataNormal];
                }];
                [ws.navigationController pushViewController:vc animated:YES];
            }else if (indexpath.row == 1){
                AddAlipayVC *vc = [AddAlipayVC new];
                vc.payTypeStr = @"alipay";
                [vc setAddSuccessBlcok:^{
                    [ws.vm.dataArray removeAllObjects];
                    [ws.vm getRequestDataNormal];
                }];
                [ws.navigationController pushViewController:vc animated:YES];
            }else if (indexpath.row == 2){
                AddAlipayVC *vc = [AddAlipayVC new];
                vc.payTypeStr = @"wechat";
                [vc setAddSuccessBlcok:^{
                    [ws.vm.dataArray removeAllObjects];
                    [ws.vm getRequestDataNormal];
                }];
                [ws.navigationController pushViewController:vc animated:YES];
            }
        }];
    }
    return _addpayView;
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
